#include "FilledTriangle.h"
#include <QDebug>

FilledTriangle::FilledTriangle()
{
    m_texture = 0;
}

FilledTriangle::FilledTriangle(QList<QPoint*> vertexs)
{
    m_texture = 0;
    m_vertexs = vertexs;
    calcHeight();
}

FilledTriangle::FilledTriangle(QList<QPoint*>vertexs, QList<QColor*>colors)
{
    m_texture = 0;
    m_vertexs = vertexs;
    m_colors = colors;
    calcWidth();
    calcHeight();
}

void FilledTriangle::draw(QPainter * painter)
{
    QPoint* p1 = vertexs().at(0);
    QPoint* p2 = vertexs().at(1);
    QPoint* p3 = vertexs().at(2);

    //qDebug() << "( " << p1->x() << ", " << p1->y() << ")" << endl;
    //qDebug() << "( " << p2->x() << ", " << p2->y() << ")" << endl;
    //qDebug() << "( " << p3->x() << ", " << p3->y() << ")" << endl;


    if(texture() && !texture()->isSetten())
    {
        int xMin = min(p1->x(),p2->x());
        xMin = min(xMin,p3->x());
        int yBase = max(p1->y(),p2->y());
        yBase = max(yBase,p3->y());
        texture()->setObjectValues(xMin,yBase,width(),height());
    }

    Line* line;
    Line* l1 = new Line(p1,p2);
    l1->setColors(colors().mid(0,2));
    Line* l2 = new Line(p1,p3);

    l2->setColors(colors().mid(0,1) << colors().mid(2,1));//Problema aqui.
    Line* l3 = new Line(p2,p3);
    l3->setColors(colors().mid(1,2));

    QList<Line*> borderLines;
    borderLines.append(l1);
    borderLines.append(l2);
    borderLines.append(l3);

    sortLines(borderLines);
    QList<QPoint*> pointsL1;
    QList<QPoint*> pointsL2;

    if(QVector2D::dotProduct(*(borderLines.at(0)->toVector2D()),*(borderLines.at(2)->toVector2D())))
    {
        pointsL1 = borderLines.at(0)->pixelCoordenates();
    }

    if(QVector2D::dotProduct(*(borderLines.at(1)->toVector2D()),*(borderLines.at(2)->toVector2D())))
    {
        pointsL2 = borderLines.at(1)->pixelCoordenates();
    }

    QList<QPoint*> projectedPoints;

    if(!pointsL1.isEmpty())
    {
        projectedPoints = borderLines.at(2)->projectedPoints(pointsL1);
    }

    if(!pointsL2.isEmpty())
    {
        projectedPoints.append(borderLines.at(2)->projectedPoints(pointsL2));
    }

    QList<QPoint*>::iterator point3 = projectedPoints.begin();
    QList<QColor*>* interpolatedColors;
    foreach(QPoint* point1,pointsL1)
    {
        interpolatedColors = new QList<QColor*>();
        line = new Line(point1,*point3);
        interpolatedColors->append(new QColor(l1->getInterpolatedColor(point1)));
        interpolatedColors->append(new QColor(l3->getInterpolatedColor(*point3)));
        line->setColors(*interpolatedColors);
        if(texture() != 0) line->setTexture(texture());
        line->draw(painter);
        point3++;
    }
    foreach(QPoint* point2,pointsL2)
    {
        interpolatedColors = new QList<QColor*>();
        line = new Line(point2,*point3);
        interpolatedColors->append(new QColor(l2->getInterpolatedColor(point2)));
        interpolatedColors->append(new QColor(l3->getInterpolatedColor(*point3)));
        line->setColors(*interpolatedColors);
        if(texture() != 0) line->setTexture(texture());
        line->draw(painter);
        point3++;
    }
}

void FilledTriangle::sortVertexs()
{
    for(int i=0;i<3;i++)
    {
        for(int j=i+1;j<3;j++)
        {
            if(pointLessThan(m_vertexs.at(j),m_vertexs.at(i))) m_vertexs.swap(i,j);
        }
    }
}

void FilledTriangle::sortLines(QList<Line*> lines)
{
    for(int i=0;i<3;i++)
    {
        for(int j=i+1;j<3;j++)
        {
            if(lines.at(j)->toVector2D()->length() < lines.at(i)->toVector2D()->length())
            {
                lines.swap(i,j);
            }
        }
    }
}

float FilledTriangle::width()
{
    return m_width;
}

float FilledTriangle::height()
{
    return m_height;
}

void FilledTriangle::calcWidth()
{
    QPoint* p1 = vertexs().at(0);
    QPoint* p2 = vertexs().at(1);
    QPoint* p3 = vertexs().at(2);

    Line* l1 = new Line(p1,p2);
    Line* l2 = new Line(p1,p3);
    Line* l3 = new Line(p2,p3);

    QList<Line*> borderLines;
    borderLines.append(l1);
    borderLines.append(l2);
    borderLines.append(l3);

    sortLines(borderLines);

    m_width = borderLines.back()->toVector2D()->length();
}

void FilledTriangle::calcHeight()
{
    QPoint* p1 = vertexs().at(0);
    QPoint* p2 = vertexs().at(1);
    QPoint* p3 = vertexs().at(2);

    Line* l1 = new Line(p1,p2);
    Line* l2 = new Line(p1,p3);
    Line* l3 = new Line(p2,p3);

    QList<Line*> borderLines;
    borderLines.append(l1);
    borderLines.append(l2);
    borderLines.append(l3);

    sortLines(borderLines);

    float c = borderLines.first()->toVector2D()->length();
    float n = projectFromZeroUpToOne(*borderLines.first()->toVector2D(),*borderLines.back()->toVector2D()).length();
    m_height = qSqrt(qPow(c,2) + qPow(n,2));
}
